package com.burgeon.backend.EnterpriseWeChat.rest.VipInfo;

import com.burgeon.backend.EnterpriseWeChat.entity.BeautyFeature;
import com.burgeon.backend.EnterpriseWeChat.entity.VipFeature;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class queryBeautyFeature extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public queryBeautyFeature(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<BeautyFeature> getData() throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<BeautyFeature> features = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            BeautyFeature beautyFeature= new BeautyFeature();
        if(jsonArray.length()>0){

            beautyFeature.setName(jsonArray.getJSONArray(i).getString(0));
            beautyFeature.setDimflag(jsonArray.getJSONArray(i).getString(1));

        }
            features.add(beautyFeature);
        }

        return features;
    }

    private MultiValueMap Data() {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段
        colums.add("NAME");
        colums.add("DIMFLAG");

        params.put("columns", colums);

        params.put("table", "C_VIPATTRIBDEF");//表名
        params.put("count","true");

        HashMap paramss = new HashMap();
//        paramss.put("column","NAME ");ID
//        paramss.put("condition","'"+name+"'");

//        params.put("params",paramss);

        HashMap expr1 = new HashMap();
        HashMap expr11 = new HashMap();
        HashMap expr111 = new HashMap();
        HashMap expr112 = new HashMap();
        HashMap expr12 = new HashMap();

        expr112.put("column", "DIMFLAG");
        expr112.put("condition", "DIM27");

        expr111.put("column", "DIMFLAG");
        expr111.put("condition", "DIM28");

        expr11.put("expr1", expr111);
        expr11.put("expr2", expr112);
        expr11.put("combine", "or");

        expr12.put("column", "DIMFLAG");
        expr12.put("condition", "DIM29");

        expr1.put("expr1", expr11);
        expr1.put("expr2", expr12);
        expr1.put("combine", "or");
;
        HashMap expr2 = new HashMap();
        HashMap expr21 = new HashMap();
        HashMap expr211 = new HashMap();

        expr21.put("column", "DIMFLAG");
        expr21.put("condition", "DIM30");
        expr211.put("column", "DIMFLAG");
        expr211.put("condition", "DIM31");

        expr2.put("expr1", expr21);
        expr2.put("expr2", expr211);
        expr2.put("combine", "or");

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);
        paramss.put("combine", "or");

        HashMap orderby = new HashMap();
        orderby.put("column", "DIMFLAG");
        orderby.put("asc", "true");

        List list = new ArrayList();
        list.add(orderby);
        params.put("orderby",list);
        params.put("params",paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        queryBeautyFeature g = new queryBeautyFeature("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {//3125
            System.err.println(g.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
